package com.wanxi.education.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanxi.education.mapper.CourseFrontMapper;
import com.wanxi.education.model.pojo.History;
import com.wanxi.education.model.vo.catalogue.CatalogueVo;
import com.wanxi.education.model.vo.course.*;
import com.wanxi.education.model.vo.history.HistoryAcceptVo;
import com.wanxi.education.model.vo.history.HistoryVo;
import com.wanxi.education.service.CourseFrontService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CourseFrontServiceImpl implements CourseFrontService {

    @Autowired
    private CourseFrontMapper courseFrontMapper;
    @Autowired
    private CourseFrontService courseFrontService;


    @Override
    public PageInfo<CourseHomeVo> findPageForHome(CourseFindPageVo courseFindPageVo) {
        PageHelper.startPage(courseFindPageVo.getPageNum(), courseFindPageVo.getPageSize());
        List<CourseHomeVo> pageForHome = courseFrontMapper.findPageForHome(courseFindPageVo);
        PageInfo<CourseHomeVo> pageInfo = new PageInfo<>(pageForHome);
        return pageInfo;
    }

    @Override
    public CourseByIdVo findByIdToHome(Long courseId) {
        CourseByIdVo byIdToHome = courseFrontMapper.findByIdToHome(courseId);
        Integer points = 1;
        courseFrontMapper.addCoursePoint(courseId,points);
        return byIdToHome;
    }

    @Override
    @Transactional
    public List<CatalogueVo> findCatalogueByCourseId(Long courseId) {
        List<CatalogueVo> lists = courseFrontMapper.findCatalogueByCourseId(courseId,0L);
        lists.forEach((list)->{
            list.setList(courseFrontMapper.findCatalogueByCourseId(courseId,list.getId()));
        });
        return lists;
    }

    @Override
    public PageInfo<CourseHomeVo> findPageForPersonage(CourseFindPageForPersonageVo courseFindPageForPersonageVo) {
        PageHelper.startPage(courseFindPageForPersonageVo.getPageNum(),courseFindPageForPersonageVo.getPageNum());
        List<CourseHomeVo> pageForPersonage = courseFrontMapper.findPageForPersonage(courseFindPageForPersonageVo.getUserId());
        PageInfo<CourseHomeVo> pageInfo = new PageInfo<>(pageForPersonage);
        return pageInfo;
    }

    @Override
    public void addHistory(AddHistoryVo addHistoryVo,Long userId) {
        History history = courseFrontMapper.findByCatalogueId(addHistoryVo.getCatalogueId());
        history.setUserId(userId);
        history.setPlayTime(addHistoryVo.getPlayTime());
        History historya = courseFrontMapper.findByUserIdAndCatalogueId(addHistoryVo.getCatalogueId(),userId);
        if(historya==null){
            courseFrontMapper.addHistoryVo(history);
        }else {
            history.setId(historya.getId());
            courseFrontMapper.updateHistoryVo(history);
        }

    }

    @Override
    public List<CatalogueVo> findCatalogueByCourseIdForPersonage(Long courseId) {
        List<CatalogueVo> lists = courseFrontService.findCatalogueByCourseId(courseId);
        lists.forEach((list)->{
            list.getList().forEach((lis)->{
                lis.setTime(courseFrontMapper.findVByIdTime(lis.getVideoId()));
            });
        });
        return lists;
    }

    @Override
    public List<HistoryVo> findHistoryForPersonage(HistoryAcceptVo historyAcceptVo) {
        return courseFrontMapper.findHistoryForPersonage(historyAcceptVo);
    }

    @Override
    public List<CourseHomeVo> findCarouselForHome() {
        return courseFrontMapper.findCarouselForHome();
    }

}
